﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Windows;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using YCAAUTOEK;
using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    public partial class YCAAUTOEKData : BaseFunction
    {
        public string ITXMANDANT = "00", ITXDBVERSION = "50.24", ITXUSER = "INTEXMAP", ITXDATE = "20120723";
        public string ITXLOOMTYPE = "T610  ";
        public string ITXFART = "                    ", ITXGART = "                    ", ITXBART = "                    ";
        public string ITXFVAR = "  ", ITXGVAR = "  ", ITXBVAR = "1 ";
        public string ITXFCOLOR = "          ", ITXGCOLOR = "          ", ITXBCOLOR = "          ";
        public string ITXFFINISH = "          ", ITXCADID = "1";
        public string ITXWBLO = "TX", ITXWBLB = "01";
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public string CADCHAR = " ", ITXCHAR = " ";
        DataRow dwIntexStd = null;
        public Dictionary<string, object> CADTable = new Dictionary<string, object>();
        public Convert2Intex Convert2Intex;
        public DataSet SetExt;
        INTEXDBC INTEXDBC;
        YCAAUTOEK2INTEX m_wintex;
        public YCAAUTOEKData()
            : base()
        {
        }
        public YCAAUTOEKData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            SetExt = new DataSet();
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
        }
        public YCAAUTOEKData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public void AutoRun()
        {
            DataSet SetJob = new DataSet();
            while (true)
            {
                bool last = false;
                DataSet setResult = GetJob(ref last);
                SetJob.Merge(setResult);
                AutoConvert(SetJob);
                if (last)
                    break;
            }
        }
        public DataSet ResetSetInterface()
        {
            DataSet SReturn = new DataSet();
            DataSet setResult = new DataSet();
            string filePath = Path.Combine(PathHelper.ApplicationPath, "Report\\Interface\\EKMAPINTEX.XML");
            setResult.ReadXml(filePath);
            SReturn.Merge(setResult);
            return SReturn;
        }
        public void AutoConvert(DataSet jobdata)
        {
            //<VKKAUFKRS> VKKAUFLFD> VKKNEUBEN>
            try
            {
                DataSet setResult = new DataSet();
                bool result = true;
                SetExt = jobdata;
                SetExt.Merge(ResetSetInterface());
                //m_Data.SetExt.Merge(SetData);
                bool success = true;
                string failreason = "";
                setResult = ConvertToIntex(ref success, ref failreason);
                m_wintex = new YCAAUTOEK2INTEX(OracleDB, proxy);
                m_wintex.ReSet(setResult, null);
                result = m_wintex.Execute(null);
                EventDispatcher.Instance.RaiseStatus("保存到Intex:", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            }
        } 
        public DataSet ValidateEK(DataRow dwInput, ref bool IsValid)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            dwInput["EKPMANDANT"] = "00";
            string client = dwInput["EKPMANDANT"].ToString();
            string krs = dwInput["EKPAUFKRS"].ToString();
            string lfd = dwInput["EKPAUFLFD"].ToString();             
            Result = INTEXDBC.QueryEKKOPF(dwInput,3);
            Return.Merge(Result);
            if (m_wintex == null)
            {
                m_wintex = new YCAAUTOEK2INTEX(OracleDB, proxy);
            }
            if (!Return.Tables.Contains("EKKOPF"))
            {
               m_wintex.Delete("00", krs, lfd, "000");
                MessageBox.Show("采购单" + krs + lfd + " 无效，强制删除,请立即联系程序员");
                return Return;
            }
            if (Return.Tables["EKKOPF"].Rows.Count == 0)
            {
                m_wintex.Delete("00", krs, lfd, "000");
                MessageBox.Show("采购单" + krs + lfd + " 无效，强制删除,请立即联系程序员");
                return Return;
            }
            IsValid = true;
            return Return;
        }
        #region=============INTEX WS/BS=============================
        public DataSet ResetSetDispo(string setID = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                ArrayList tables = new ArrayList();
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.xls");
                if (!System.IO.File.Exists(filePath))
                {
                    return setReturn;
                }
                if (exAgent == null)
                {
                    exAgent = new ExcelX.ExcelLib.ExcelAgent(filePath);
                }
                setResult = exAgent.LoadDataTableDef(filePath, "");
                filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                setResult.WriteXml(filePath);
                setResult = new DataSet();
                setResult = LoadSetDispo(setID);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadSetDispo(string setID = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\PRODSETUP.XML");
                if (!System.IO.File.Exists(filePath))
                {
                    return setReturn;
                }
                setResult.ReadXml(filePath);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SaveSetDispo(DataTable tbDispo)
        {
            DataSet setDispo = new DataSet();

            try
            {
                setDispo.Merge(tbDispo);
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\DATADISPO.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                    setDispo.WriteXml(filePath);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion
        public DataSet ConvertToIntex(ref bool success, ref string failreason)
        {
            DataSet setResult = new DataSet();
            DataSet Return = new DataSet();//is used to magage the data to upload to intex 
            DataTable tbGSCOLOR = new DataTable();

            try
            {
                Convert2Intex = new Convert2Intex(SetExt, dwIntexStd, OracleDB, proxy);
                dwIntexStd = Convert2Intex.dwIntexStd;
                foreach (DataRow rw in SetExt.Tables["EKPOS_EXT"].Rows)
                {
                    setResult = Convert2EKPOS(rw);
                    Return.Merge(setResult);
                }
                foreach (DataRow rw in SetExt.Tables["EKKOPF_EXT"].Rows)
                {
                    setResult = Convert2EKKOPF(rw);
                    Return.Merge(setResult);
                    if(setResult.Tables[0].Rows.Count==0)
                    {
                    TraceMessage+=SetExt.Tables["EKKOPF_EXT"].Rows[0]["EKKAUFKRS"].ToString()+SetExt.Tables["EKKOPF_EXT"].Rows[0]["EKKAUFLFD"].ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                success = false;
                Tracer.Error(ex.StackTrace, ex);
               TraceMessage+=ex.StackTrace+Environment.NewLine+ex.Message.ToString();
            }
            return Return;
        }
        public DataSet Convert2EKPOS(DataRow dwData)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "EKPOS_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "EKPOS";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "EKPOS"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwData);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2EKKOPF(DataRow dwData)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "EKKOPF_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "EKKOPF";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim().Trim() == "EKKOPF"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwData);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
    }
}